package com.cashmate.struts;

import com.cashmate.hibernate.dao.AreaDAO;
import com.cashmate.hibernate.dao.BorrowerDAO;
import com.cashmate.hibernate.dao.BranchDAO;
import com.cashmate.hibernate.pojo.Borrowers;
import com.cashmate.hibernate.pojo.Employees;
import com.cashmate.hibernate.pojo.Usertypes;
import com.opensymphony.xwork2.ActionSupport;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.ServletActionContext;

public class EditBorrowers extends ActionSupport {

    private static Logger logger = Logger.getLogger(EditBorrowers.class);
    //This properties have setters below... The setters are called and populated by the grid.
    private String id;//the column in grid which we set key=true
    private String firstname;
    private String middlename;
    private String lastname;
    private String religion;
    private String civilstatus;
    private String dateofbirth;
    private String placeofbirth;
    private String citizenship;
    private String streetaddress;
    private String barangay;
    private String cityormunicipality;
    private String province;
    private String postalcode;
    private String contactnumber;
    private String age;
    private String branchname;
    private String areacode;
    private BorrowerDAO borowDao = (BorrowerDAO) ServletActionContext.getServletContext().getAttribute("borrowerDAO");
    private final BranchDAO branches = (BranchDAO) ServletActionContext.getServletContext().getAttribute("branchDAO");
    private final AreaDAO areas = (AreaDAO) ServletActionContext.getServletContext().getAttribute("areaDAO");
    private Employees emps = (Employees) ServletActionContext.getRequest().getSession().getAttribute("employee");
    private Usertypes ut = (Usertypes) ServletActionContext.getRequest().getSession().getAttribute("usertype");

    //Notice we don't have a success result because we do not need to redirect the user when he/she updates the dbase
    //We redirect him/her only if an error occurred.
    @Action(value = "/edit-borrower", results = {
        @Result(name = "error", type = "redirectAction", location = "../error/messages.jsp")
    })
    @Override
    public String execute() throws Exception {
        Borrowers b = borowDao.get(Integer.parseInt(id));
        try {
            if (firstname != null) {
                b.setFirstname(firstname);
                logger.info(ut.getType() + " (" + emps.getIdnumber() + ") updated Borrower(" + b.getBorrowercode() + ") with value: firstname:" + firstname);
            }
            if (middlename != null) {
                b.setMiddlename(middlename);
                logger.info(ut.getType() + " (" + emps.getIdnumber() + ") updated Borrower(" + b.getBorrowercode() + ") with value: middlename:" + middlename);
            }
            if (lastname != null) {
                b.setLastname(lastname);
                logger.info(ut.getType() + " (" + emps.getIdnumber() + ") updated Borrower(" + b.getBorrowercode() + ") with value: lastname:" + lastname);
            }
            if (religion != null) {
                b.setReligion(religion);
                logger.info(ut.getType() + " (" + emps.getIdnumber() + ") updated Borrower(" + b.getBorrowercode() + ") with value: religion:" + religion);
            }
            if (civilstatus != null) {
                b.setCivilstatus(civilstatus);
                logger.info(ut.getType() + " (" + emps.getIdnumber() + ") updated Borrower(" + b.getBorrowercode() + ") with value: civilstatus:" + civilstatus);
            }
            if (dateofbirth != null) {
                b.setDateofbirth(dateofbirth);
                logger.info(ut.getType() + " (" + emps.getIdnumber() + ") updated Borrower(" + b.getBorrowercode() + ") with value: dateofbirth:" + dateofbirth);
            }
            if (placeofbirth != null) {
                b.setPlaceofbirth(placeofbirth);
                logger.info(ut.getType() + " (" + emps.getIdnumber() + ") updated Borrower(" + b.getBorrowercode() + ") with value: placeofbirth:" + placeofbirth);
            }
            if (citizenship != null) {
                b.setCitizenship(citizenship);
                logger.info(ut.getType() + " (" + emps.getIdnumber() + ") updated Borrower(" + b.getBorrowercode() + ") with value: citizenship:" + citizenship);
            }
            if (streetaddress != null) {
                b.setStreetaddress(streetaddress);
                logger.info(ut.getType() + " (" + emps.getIdnumber() + ") updated Borrower(" + b.getBorrowercode() + ") with value: streetaddress:" + streetaddress);
            }
            if (barangay != null) {
                b.setBarangay(barangay);
                logger.info(ut.getType() + " (" + emps.getIdnumber() + ") updated Borrower(" + b.getBorrowercode() + ") with value: barangay:" + barangay);
            }
            if (province != null) {
                b.setProvince(province);
                logger.info(ut.getType() + " (" + emps.getIdnumber() + ") updated Borrower(" + b.getBorrowercode() + ") with value: province:" + province);
            }
            if (postalcode != null) {
                b.setPostalcode(postalcode);
                logger.info(ut.getType() + " (" + emps.getIdnumber() + ") updated Borrower(" + b.getBorrowercode() + ") with value: postalcode:" + postalcode);
            }
            if (contactnumber != null) {
                b.setContactnumber(contactnumber);
                logger.info(ut.getType() + " (" + emps.getIdnumber() + ") updated Borrower(" + b.getBorrowercode() + ") with value: contactnumber:" + contactnumber);
            }
            if (age != null) {
                b.setAge(age);
                logger.info(ut.getType() + " (" + emps.getIdnumber() + ") updated Borrower(" + b.getBorrowercode() + ") with value: age:" + age);
            }
            if (branchname != null) {
                if (!branchname.equals("-1")) {
                    b.setBranches(branches.get(Integer.parseInt(branchname)));
                    logger.info(ut.getType() + " (" + emps.getIdnumber() + ") updated Borrower(" + b.getBorrowercode() + ") with value: branch:" + b.getBranchname());
                } else {
                    ServletActionContext.getResponse().sendError(HttpServletResponse.SC_NOT_ACCEPTABLE, "Please select a Branch.");
                }
            }
            if (areacode != null) {
                if (!areacode.equals("-1")) {
                    b.setAreas(areas.get(Integer.parseInt(areacode)));
                    logger.info(ut.getType() + " (" + emps.getIdnumber() + ") updated Borrower(" + b.getBorrowercode() + ") with value: area:" + b.getAreacode());
                } else {
                    ServletActionContext.getResponse().sendError(HttpServletResponse.SC_NOT_ACCEPTABLE, "Please select an Area.");
                }
            }
            if (cityormunicipality != null) {
                b.setCityormunicipality(cityormunicipality);
                logger.info(ut.getType() + " (" + emps.getIdnumber() + ") updated Borrower(" + b.getBorrowercode() + ") with value: cityormunicipality:" + cityormunicipality);
            }
            borowDao.update(b);
        } catch (Exception e) {
            if (emps != null) {
                logger.fatal("(" + emps.getIdnumber() + "):" + e.getLocalizedMessage(), e);
            } else {
                logger.fatal(e.getLocalizedMessage(), e);
                e.printStackTrace();
            }
            addActionError(e.getLocalizedMessage());
            return ERROR;
        }
        return NONE;
    }
    //setters

    public void setAge(String age) {
        this.age = age;
    }

    public void setAreacode(String areacode) {
        this.areacode = areacode;
    }

    public void setBarangay(String barangay) {
        this.barangay = barangay;
    }

    public void setBranchname(String branchname) {
        this.branchname = branchname;
    }

    public void setCitizenship(String citizenship) {
        this.citizenship = citizenship;
    }

    public void setCityormunicipality(String cityormunicipality) {
        this.cityormunicipality = cityormunicipality;
    }

    public void setCivilstatus(String civilstatus) {
        this.civilstatus = civilstatus;
    }

    public void setContactnumber(String contactnumber) {
        this.contactnumber = contactnumber;
    }

    public void setDateofbirth(String dateofbirth) {
        this.dateofbirth = dateofbirth;
    }

    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }

    public void setId(String id) {
        this.id = id;
    }

    public void setLastname(String lastname) {
        this.lastname = lastname;
    }

    public void setMiddlename(String middlename) {
        this.middlename = middlename;
    }

    public void setPlaceofbirth(String placeofbirth) {
        this.placeofbirth = placeofbirth;
    }

    public void setPostalcode(String postalcode) {
        this.postalcode = postalcode;
    }

    public void setProvince(String province) {
        this.province = province;
    }

    public void setReligion(String religion) {
        this.religion = religion;
    }

    public void setStreetaddress(String streetaddress) {
        this.streetaddress = streetaddress;
    }
}
